package com.yulebaby.callsenter.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;

import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.utils.CommonUtil;
import com.yulebaby.callsenter.utils.ToastUtil;

/**
 * Author: Zhangbo
 * Time: 2016/12/06
 * 选择的对话框,默认从底部弹出
 */
public class PickerDialog extends Dialog implements View.OnClickListener, YuLeBaByPickerView.OnValueChangeListener, YuLeBaByPickerView.OnScrollListener {
    private Context context;
    private YuLeBaByPickerView mYuLeBaByPickerView;
    private Button mBtnconfirm;
    private ContentListener mListener;

    public PickerDialog(Context context) {
        super(context, R.style.PickerDialogStyle);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = View.inflate(getContext(), R.layout.dialog_picker, null);

        mYuLeBaByPickerView = (YuLeBaByPickerView) view.findViewById(R.id.picker);
        mBtnconfirm = (Button) view.findViewById(R.id.btn_confirm);
        mBtnconfirm.setOnClickListener(this);
        mYuLeBaByPickerView.setOnScrollListener(this);
        mYuLeBaByPickerView.setOnValueChangedListener(this);
        mYuLeBaByPickerView.setWrapSelectorWheel(false);
        //让自定义对话框显示在界面的底部

        setContentView(view);
        Window window = getWindow();
        LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = metrics.widthPixels;
        layoutParams.height = CommonUtil.getDimens(R.dimen.pick_dialog_heigh);
        window.setAttributes(layoutParams);
    }


    @Override
    public void onValueChange(YuLeBaByPickerView picker, int oldVal, int newVal) {
        String[] content = picker.getDisplayedValues();
        if (content != null) {
            String scrollContent = content[newVal - picker.getMinValue()];
        }
    }

    @Override
    public void onScrollStateChange(YuLeBaByPickerView view, int scrollState) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                getCurrentContent();
                dismiss();
                break;
        }
    }

    /**
     * 设置选择的数据
     *
     * @param mDisplayValues
     */
    public void initPicker(String[] mDisplayValues) {
        mYuLeBaByPickerView.refreshByNewDisplayedValues(mDisplayValues);
    }


    /**
     * 获取列表中显示的内容
     */
    private void getCurrentContent() {
        String[] content = mYuLeBaByPickerView.getDisplayedValues();
        if (content != null) {
            String currentContent = content[mYuLeBaByPickerView.getValue() - mYuLeBaByPickerView.getMinValue()];
            if (mListener != null) {
                mListener.select(currentContent);
            }
        }
    }

    public void setCurrentContentListener(ContentListener listener) {
        mListener = listener;
    }

    public interface ContentListener {
        void select(String result);
    }
}
